<script lang="ts" setup>
  import { departmentListApi } from '@/pages/apis/consult'
  import type { DepartData } from '@/types/consult'
  import { objectToQueryParams } from '@/utils'
  import { computed, ref } from 'vue'

  const list = ref<DepartData[]>([])

  const activeIndex = ref(0)
  const getData = async () => {
    const res = await departmentListApi()
    list.value = res.filter((item) => item.child.length > 0)
  }
  getData()

  const childList = computed(() => {
    // 🔴 注意：数据还没请求回来时，没有 .child 属性，这里需要用可选链接安全访问
    return list.value[activeIndex.value]?.child
  })

  const pageParams = defineProps<{ type: string; illnessType: string }>()
</script>

<template>
  <view class="department-page">
    <scroll-view scroll-y class="department-primary">
      <view
        v-for="(item, index) in list"
        :key="item.id"
        class="department-item"
        :class="{ active: activeIndex === index }"
        @click="activeIndex = index"
      >
        {{ item.name }}
      </view>
    </scroll-view>
    <scroll-view class="department-secondary">
      <navigator
        v-for="item in childList"
        :key="item.id"
        hover-class="none"
        class="department-item"
        :url="`/subpkg_consult/description/index?${objectToQueryParams({
          ...pageParams,
          depId: item.id,
        })}`"
      >
        {{ item.name }}
      </navigator>
    </scroll-view>
  </view>
</template>

<style lang="scss">
  @import './index.scss';
</style>
